#!/usr/bin/env bash

_arch=$(uname -m)
_date=$(date +'%Y%m%d')
_filename=${_arch}-msys2-${_date}.tar.xz
_log=/tmp/compress.log

echo "Creating MSYS2 archive /tmp/$_filename"
if [ -f /tmp/$_filename ]; then
  rm -f /tmp/$_filename
fi

_dirs=
for curr_dir in /etc /include /lib /libexec /sbin /share /ssl /tmp /usr /var /mingw32_shell.bat /mingw64_shell.bat /msys2.ico /msys2_shell.bat ; do
  if [[ -d $curr_dir || -f $curr_dir ]]; then
    _dirs="$_dirs $curr_dir"
  fi
done

if [ -n "$_dirs" ]; then
  _compress_cmd="/usr/bin/tar --transform='s/:/_/g' --dereference --hard-dereference -cvJf /tmp/${_filename} $_dirs -X /etc/compress_exclude"
  cd /
  eval ${_compress_cmd} | tee $_log 2>&1
  _result=$?
  if [ "$_result" -eq "0" ]; then
  echo " done"
  else
  die "MSYS2 compressing fail. See $_log"
  fi
fi

exit 0
